package com.wanwen.controller;

import java.util.Date;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wanwen.dto.SearchCon;
import com.wanwen.entity.Users;
import com.wanwen.mapper.UserMapper;
import com.wanwen.utils.JwtUtils;
import com.wanwen.utils.Result;
import com.wanwen.utils.ResultData;
import com.wanwen.utils.StringUtils;

@RestController
@RequestMapping("/users")
public class UserController {
	@Autowired
	private UserMapper userMapper;

	/**
	 * 多条件分页查询
	 * 
	 * @param usc 条件对象
	 * @return
	 */
	@RequestMapping("/getAllbyLimit")
	public Object showAllByCon(SearchCon usc) {
		// 封装条件查询构造器
		QueryWrapper<Users> wrapper = new QueryWrapper<Users>();
		wrapper.like(StringUtils.isnotNull(usc.getUserName()), "uname", usc.getUserName());
		wrapper.eq(usc.getIsLock() != null, "is_lock", usc.getIsLock());
		wrapper.ge(StringUtils.isnotNull(usc.getBeginDate()), "create_time", usc.getBeginDate());
		wrapper.le(StringUtils.isnotNull(usc.getEndDate()), "create_time", usc.getEndDate());
		// 封装分页对象
		Page<Users> page = new Page<Users>(usc.getPage(), usc.getLimit());
		Page<Users> userPage = userMapper.selectPage(page, wrapper);

		return new ResultData(0, "", userPage.getRecords(), userPage.getTotal());
	}

	/**
	 * 用户登录接口
	 * 
	 * @param un  用户
	 * @param pwd 密码
	 * @return
	 */
	@RequestMapping("/login")
	public Object login(String un, String pwd, HttpServletResponse response) {
		// 允许跨域的主机地址
		response.setHeader("Access-Control-Allow-Origin", "*");
		// 1、根据用户名从系统中查找用户
		QueryWrapper<Users> wrapper = new QueryWrapper<Users>();
		wrapper.eq("uname", un);
		Users user = userMapper.selectOne(wrapper);
		if (user == null) {
			return new Result("用户名不存在", null, false);
		} else {
			// 2、判断是否锁定
			if (user.getIsLock() == 1) {
				return new Result("用户被锁定", null, false);
			} else {
				// 3、判断密码是否正确
				if (user.getPassword().equals(pwd)) {
					// 4、需要生成一个tokenId,然后返回给客户端
					String tokenId = JwtUtils.getToken(un);
					UpdateWrapper<Users> updateWrapper = new UpdateWrapper<>();
					updateWrapper.set("wrong_num", 0).set("last_login_time", new Date()).eq("id", user.getId());
					userMapper.update(user, updateWrapper);
					return new Result(tokenId,user , true);
				} else {
					UpdateWrapper<Users> updateWrapper = new UpdateWrapper<>();
					if (user.getWrongNum() == null || user.getWrongNum() <5) {
						updateWrapper.set("wrong_num", user.getWrongNum() == null ? 1 : user.getWrongNum() + 1).eq("id",
								user.getId());
					} else {
						updateWrapper.set("is_lock", 1).set("wrong_num", user.getWrongNum() == null ? 1 : user.getWrongNum() + 1).set("lock_time", new Date()).eq("id", user.getId());
					}
					userMapper.update(user, updateWrapper);
					return new Result("密码不正确", null, false);
				}
			}
		}

	}

	/**
	 * /users/update 锁定和解锁用户
	 * 
	 * @param users
	 * @return rs
	 */
	@RequestMapping("/updateLock")
	public Object updateLock(Users users) {
		UpdateWrapper<Users> updateWrapper = new UpdateWrapper<>();
		updateWrapper.set("is_lock", users.getIsLock()).set("wrong_num", 0).set("lock_time", new Date()).eq("id", users.getId());
		int update = userMapper.update(users, updateWrapper);
		Result rs = new Result(users.getIsLock() == 1 ? "锁定成功" : "解锁成功", "", update == 1 ? true : false);
		return rs;
	}

	/**
	 * /users/update 重置用户密码
	 * @param users
	 * @return rs
	 */
	@RequestMapping("/resetPassword")
	public Object resetPassword(Users users) {
		UpdateWrapper<Users> updateWrapper = new UpdateWrapper<>();
		updateWrapper.set("password", "hgd2003").eq("id", users.getId());
		int update = userMapper.update(users, updateWrapper);
		return new Result(null, "", update == 1 ? true : false);
	}

	/**
	 * /users/update 修改用户密码
	 * @param users
	 * @return rs
	 */
	@RequestMapping("/updatePassword")
	public Object updatePassword(Users users,String passwordOld) {
		// 1、根据用户名从系统中查找用户
				QueryWrapper<Users> wrapper = new QueryWrapper<Users>();
				wrapper.eq("uname", users.getUname());
				Users user = userMapper.selectOne(wrapper);
				if (user == null) {
					return new Result("用户名不存在", null, false);
				} else {
					// 2、判断是否锁定
					if (user.getIsLock() == 1) {
						return new Result("用户被锁定", null, false);
					} else {
						// 3、判断密码是否正确
						if (user.getPassword().equals(passwordOld)) {
							UpdateWrapper<Users> updateWrapperpwd = new UpdateWrapper<>();
							updateWrapperpwd.set("password", users.getPassword()).eq("id", user.getId());
							int update = userMapper.update(user, updateWrapperpwd);
							return new Result(null, "", update == 1 ? true : false);
						} else {
							return new Result("原密码不正确", null, false);
						}
					}
				}
		
	}

	/**
	 * /users/add 添加用户
	 * 
	 * @param users
	 * @return rs
	 */
	@RequestMapping("/add")
	public Object add(Users users) {
		// 1、根据用户名从系统中查找用户
		QueryWrapper<Users> wrapper = new QueryWrapper<Users>();
		wrapper.eq("uname", users.getUname());
		Users user = userMapper.selectOne(wrapper);
		if (user == null) {
			Result rs = new Result("", "", userMapper.insert(users) == 1 ? true : false);
			return rs;
		} else {
			return new Result("用户名已存在", null, false);
		}
	}

	/**
	 * /users/update 修改用户
	 * 
	 * @param users
	 * @return rs
	 */
	@RequestMapping("/update")
	public Object update(Users users) {
		// 1、根据用户名从系统中查找用户
		QueryWrapper<Users> wrapper = new QueryWrapper<Users>();
		wrapper.eq("uname", users.getUname());
		Users user = userMapper.selectOne(wrapper);
		if (user == null) {
			Result rs = new Result(users.getId() + "", "", userMapper.updateById(users) == 1 ? true : false);
			return rs;
		} else {
			return new Result("用户名已存在", null, false);
		}
	}

	/**
	 * /users/del 删除用户
	 * 
	 * @param users
	 * @return rs
	 */
	@RequestMapping("/del")
	public Object del(Users users) {
		Result rs = new Result(users.getId() + "", "", userMapper.deleteById(users) == 1 ? true : false);
		return rs;
	}
	/**
	 * /users/dels 批量删除用户
	 * 
	 * @param ids
	 * @return rs
	 */
	@RequestMapping("/dels")
	public Object del(String ids) {
		System.err.println(ids);
		String[] idsStr=ids.split(",");
		int r = 0;
		for(String s:idsStr) {
			System.err.println(s);
			Users user = new Users();
			user.setId(Integer.parseInt(s));
			r += userMapper.deleteById(user);
		}
		Result rs = new Result("删除了"+r+"名用户", "", r != 0 ? true : false);
		return rs;
	}
}
